<script setup>
import api from "./api.js";
import EditDialog from "./edit.vue";
import UserDialog from "./user.vue";
import MenuDialog from "./menu.vue";


const tableData = ref([])
const tableRef = ref()
const editDialog = ref();
const userDialog = ref();
const menuDialog = ref();

function handleAdd() {
  editDialog.value.open();
}

function handleUpdate(row) {
  editDialog.value.open(row);
}

function handleDelete(row) {
  api.delete(row.id).then(() => {
    getList();
  })
}

function handleUser(row) {
  userDialog.value.open(row);
}

function handleMenu(row) {
  menuDialog.value.open(row);
}

function getList() {
  api.list().then(response => {
    tableData.value = response.rows;
  }).catch(error => {
    console.log(error);
  });
}

onMounted(()=> {
  getList();
})

</script>

<template>
  <div class="organization-structure-main" style="height: 100%;display: flex;flex-direction: column;">

    <div style="margin-bottom: 15px;">
      <el-button  @click="handleAdd">增加</el-button>
    </div>
    <vxe-table style="height: 100%;flex-grow: 1;"
        border="full"
        ref="tableRef"
        :column-config="{resizable: true}"
        :data="tableData">
      <vxe-column field="roleName" title="名称"></vxe-column>
      <vxe-column field="roleKey" title="编码"></vxe-column>
      <vxe-column field="roleSort" title="排序"></vxe-column>
      <vxe-column width="250" title="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button size="small" link type="primary" @click="handleUpdate(row)">修改</el-button>
          <el-button size="small" link type="primary" @click="handleDelete(row)">删除</el-button>
          <el-button size="small" link type="primary" @click="handleUser(row)">用户</el-button>
          <el-button size="small" link type="primary" @click="handleMenu(row)">菜单</el-button>
        </template>
      </vxe-column>
    </vxe-table>

    <EditDialog ref="editDialog" @sub="getList"></EditDialog>
    <UserDialog ref="userDialog" @sub="getList"></UserDialog>
    <MenuDialog ref="menuDialog" @sub="getList"></MenuDialog>
  </div>
</template>

<style scoped lang="scss">
  .organization-structure-main {
    box-sizing: border-box;
    padding: 15px;
  }
</style>